package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferWorkCount;
import com.tianqu.tms.www.transfer.viewhelper.TransferWorkCountViewHelper;

public class TransferWorkCountAddResultController extends TmsController {
	private Logger logger = Logger.getLogger(TransferWorkCountAddResultController.class);
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferWorkCountViewHelper view = (TransferWorkCountViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			//插数据
			TransferWorkCount entity = new TransferWorkCount();
			TmsUserProfile profile = uc.getUserProfile();
			entity.setWorkType(view.getWorkType());
			entity.setWorkCount(Integer.parseInt(view.getWorkCount()));
			if(String.valueOf(view.getNdate()).length() == 1){
				entity.setCountDay(DateUtil.getCurrentTimeStr4()+"-0"+view.getNdate());
			}else{
				entity.setCountDay(DateUtil.getCurrentTimeStr4()+"-"+view.getNdate());
			}
			entity.setUserId(Integer.parseInt(view.getUserid()));
			entity.setInsertUserId(profile.getUniqueId());
			entity.insertTransferWorkCountInfo(entity);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

}
